package com.ethink.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ethink.system.api.request.SystemRoleApiAuthorityAddRequest;
import com.ethink.system.api.request.SystemRoleApiAuthorityUpdateRequest;
import com.ethink.system.common.entity.SystemRoleApiAuthority;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ethink.system.common.mapper.SystemRoleApiAuthorityMapper;
import com.ethink.system.converter.SystemRoleApiAuthorityConverter;
import com.ethink.system.service.SystemRoleApiAuthorityService;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统角色菜单关联表 Service接口实现
 *
 * @author default
 * @date 2022-09-27
 */
@Slf4j
@Service
public class SystemRoleApiAuthorityServiceImpl extends ServiceImpl<SystemRoleApiAuthorityMapper, SystemRoleApiAuthority>
        implements SystemRoleApiAuthorityService {

    @Autowired
    private SystemRoleApiAuthorityMapper systemRoleApiAuthorityMapper;

    @Autowired
    private SystemRoleApiAuthorityConverter systemRoleApiAuthorityConverter;


    @Override
    public void savaBatch(SystemRoleApiAuthorityAddRequest request) {
        if (request.getAuthorityIdList().size() == 0){
            return;
        }
        List<SystemRoleApiAuthority> list = new ArrayList<>();
        SystemRoleApiAuthority systemRoleApiAuthority;
        for (Long aLong : request.getAuthorityIdList()) {
            systemRoleApiAuthority = new SystemRoleApiAuthority();
            systemRoleApiAuthority.setRoleId(request.getRoleId());
            systemRoleApiAuthority.setAuthorityId(aLong);
            list.add(systemRoleApiAuthority);
        }
        saveBatch(list);
    }

    @Override
    public void updateBatch(SystemRoleApiAuthorityUpdateRequest updateRequest) {
        delByRoleId(updateRequest.getRoleId());
        SystemRoleApiAuthorityAddRequest addRequest = BeanUtil.copyProperties(updateRequest, SystemRoleApiAuthorityAddRequest.class);
        savaBatch(addRequest);
    }

    @Override
    public void delByRoleId(Long roleId) {
        LambdaQueryWrapper<SystemRoleApiAuthority> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemRoleApiAuthority::getRoleId,roleId);
        systemRoleApiAuthorityMapper.delete(wrapper);
    }
}
